package com.yueyang.sojourn.server.service.sojourn.transaction;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.model.po.SojournTransaction;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 交易服务
 * @author andy.yang
 * @version v1
 * @summary
 * @Copyright (c) 2019, yueyang Group All Rights Reserved.
 * @since 2020/3/17
 */
@Service
public class SojournTransactionService {
    @Resource
    SojournTransactionMapper sojournTransactionMapper;

    /**
     * 创建交易
     * @param transaction
     * @return
     */
    public Long add(SojournTransaction transaction){
        sojournTransactionMapper.insert(transaction);
        return transaction.getId();
    }

    /**
     * 编辑交易
     * @param transaction
     * @return
     */
    public void update(SojournTransaction transaction){
        int rs = sojournTransactionMapper.update(transaction);
        if(rs <= 0){
            throw new RuntimeException("交易订单号[" + transaction.getOrderSn() + "]更新失败");
        }
    }

    /**
     * 更新预支付id和预支付人（预支付人就是支付人）
     * @param transaction
     * @return
     */
    public void updateThirdPrepayId(SojournTransaction transaction){
        int rs = sojournTransactionMapper.updateThirdPrepayId(transaction);
        if(rs <= 0){
            throw new RuntimeException("交易订单号[" + transaction.getOrderSn() + "]更新预支付id和预支付人失败");
        }
    }

    /**
     * 删除
     * @param transactionId
     * @param userId
     * @return
     */
    public int delete(Long transactionId,Long version,Long userId){
        return sojournTransactionMapper.delete(transactionId,version,userId);
    }
    /**
     * @param userId      所属用户id
     * @param pageNo      页码
     * @param pageSize    页大小
     * @return
     * @author Andy.yang
     * @summary 分页查询
     * @version v1
     * @since 2020-02-23
     */
    public PageInfo<SojournTransaction> findPage(Long userId, Integer status,int pageNo, int pageSize) {
        PageInfo<SojournTransaction> pageInfo = PageHelper.startPage(pageNo, pageSize)
                .doSelectPageInfo(() -> sojournTransactionMapper.findBy(userId,status));
        return pageInfo;
    }

    /**
     * @param transactionId      交易id
     * @return
     * @author Andy.yang
     * @summary 订单分页查询
     * @version v1
     * @since 2020-02-23
     */
    public SojournTransaction findById(Long transactionId) {
        return sojournTransactionMapper.selectByPrimaryKey(transactionId);
    }

    /**
     * 根据订单号查询付款交易
     * @param orderSn      订单号
     * @return
     * @author Andy.yang
     * @summary 订单分页查询
     * @version v1
     * @since 2020-02-23
     */
    public SojournTransaction findPayByOrderSn(String orderSn) {
        return sojournTransactionMapper.findPayByOrderSn(orderSn);
    }

    /**
     * 根据退款单号查询退款款交易
     * @param refundSn      退款号
     * @return
     * @author Andy.yang
     * @summary 订单分页查询
     * @version v1
     * @since 2020-02-28
     */
    public SojournTransaction findRefundByOrderRefundSn(String refundSn) {
        return sojournTransactionMapper.findRefundByOrderRefundSn(refundSn);
    }

    /**
     * 根据订单号查询正在退款（未完成的退款）款交易；
     * @param orderSn      退款号
     * @return
     * @author Andy.yang
     * @summary 订单分页查询
     * @version v1
     * @since 2020-02-28
     */
    public SojournTransaction findRefundingByOrderSn(String orderSn) {
        return sojournTransactionMapper.findRefundingByOrderSn(orderSn);
    }

}
